/*
 * Copyright (C) 2018 Logos Technology Ltd.
 */

package com.logos.tool.map;

/**
 * @author Young Ken.
 */
public class CoordinateTransform extends MapConstant {

    /**
     * gcj02 转 wgs84
     *
     * @param lat 纬度
     * @param lon 经度
     * @return double[] latitude, longitude
     */
    public static double[] transformLocationFromGcj02ToWgs84(double lat, double lon) {
        double[] transforms = transform(lat, lon);
        double longitude = lon * 2 - transforms[1];
        double latitude = lat * 2 - transforms[0];
        return new double[]{latitude, longitude};
    }

    /**
     * wgs84 转 gcj02
     *
     * @param lat 纬度
     * @param lon 经度
     * @return double[] latitude, longitude
     */
    public static double[] transformLocationFromWgs84ToGcj02(double lat, double lon) {
        if (outOfChina(lat, lon)) {
            return new double[]{lat, lon};
        }
        double transLat = transformLat(lon - 105.0, lat - 35.0);
        double transLon = transformLon(lon - 105.0, lat - 35.0);
        double radLat = lat / 180.0 * PI;
        double magic = Math.sin(radLat);
        magic = 1 - EE * magic * magic;
        double sqrtMagic = Math.sqrt(magic);
        transLat = (transLat * 180.0) / ((EARTH_RADIUS * (1 - EE)) / (magic * sqrtMagic) * PI);
        transLon = (transLon * 180.0) / (EARTH_RADIUS / sqrtMagic * Math.cos(radLat) * PI);
        double mgLat = lat + transLat;
        double mgLon = lon + transLon;
        return new double[]{mgLat, mgLon};
    }

    private static double[] transform(double lat, double lon) {
        if (outOfChina(lat, lon)) {
            return new double[]{lat, lon};
        }
        double transLat = transformLat(lon - 105.0, lat - 35.0);
        double transLon = transformLon(lon - 105.0, lat - 35.0);
        double radLat = lat / 180.0 * PI;
        double magic = Math.sin(radLat);
        magic = 1 - EE * magic * magic;
        double sqrtMagic = Math.sqrt(magic);
        transLat = (transLat * 180.0) / ((EARTH_RADIUS * (1 - EE)) / (magic * sqrtMagic) * PI);
        transLon = (transLon * 180.0) / (EARTH_RADIUS / sqrtMagic * Math.cos(radLat) * PI);
        double mgLat = lat + transLat;
        double mgLon = lon + transLon;
        return new double[]{mgLat, mgLon};
    }

    /**
     * out of china
     *
     * @param lat 纬度
     * @param lon 经度
     * @return in china return true
     */
    public static boolean outOfChina(double lat, double lon) {

        if (lon < 72.004 || lon > 137.8347) {
            return true;
        }
        if (lat < 0.8293 || lat > 55.8271) {
            return true;
        }
        return false;
    }

    private static double transformLat(double lon, double lat) {

        double ret = -100.0 + 2.0 * lon + 3.0 * lat + 0.2 * lat * lat + 0.1 * lon * lat
            + 0.2 * Math.sqrt(Math.abs(lon));
        ret += (20.0 * Math.sin(6.0 * lon * PI) + 20.0 * Math.sin(2.0 * lon * PI)) * 2.0 / 3.0;
        ret += (20.0 * Math.sin(lat * PI) + 40.0 * Math.sin(lat / 3.0 * PI)) * 2.0 / 3.0;
        ret += (160.0 * Math.sin(lat / 12.0 * PI) + 320 * Math.sin(lat * PI / 30.0)) * 2.0 / 3.0;
        return ret;
    }

    private static double transformLon(double lon, double lat) {

        double ret = 300.0 + lon + 2.0 * lat + 0.1 * lon * lon + 0.1 * lon * lat
            + 0.1 * Math.sqrt(Math.abs(lon));
        ret += (20.0 * Math.sin(6.0 * lon * PI) + 20.0 * Math.sin(2.0 * lon * PI)) * 2.0 / 3.0;
        ret += (20.0 * Math.sin(lon * PI) + 40.0 * Math.sin(lon / 3.0 * PI)) * 2.0 / 3.0;
        ret += (150.0 * Math.sin(lon / 12.0 * PI) + 300.0 * Math.sin(lon / 30.0 * PI)) * 2.0 / 3.0;
        return ret;
    }
}
